import 'package:flutter/material.dart';
import 'package:google_fonts/google_fonts.dart';
import 'package:zoom_tap_animation/zoom_tap_animation.dart';

/// Dimension Rules - 尺寸规则工具类
/// 提供了一系列扩展方法来简化布局和样式设置
///
/// 使用示例：
/// ```dart
/// // 1. 创建间距 - SizedBoxExtension
/// Column(
///   children: [
///     Text('Title'),
///     16.h.heightBox,  // 垂直间距 16
///     Text('Content'),
///     20.w.widthBox,   // 水平间距 20（通常用于 Row）
///     30.squareBox,    // 30x30 的正方形间距
///   ],
/// )
///
/// // 2. 设置边距 - WidgetSpacingExtension
/// Text('Hello')
///   .wrapAll(16.w)              // 所有方向 16 的内边距
///   .wrapHorizontal(20.w)       // 左右 20 的内边距
///   .wrapVertical(12.h)         // 上下 12 的内边距
///   .wrapTop(8.h)               // 仅顶部 8 的内边距
///   .wrapBottom(8.h)            // 仅底部 8 的内边距
///   .wrapLeft(16.w)             // 仅左侧 16 的内边距
///   .wrapRight(16.w)            // 仅右侧 16 的内边距
///
/// // 3. 设置文字样式 - WidgetTextExtension
/// Text('Welcome')
///   .fontSize(24.sp)                    // 字体大小 24
///   .textColor(ColorPalette.blue1)      // 文字颜色
///   .arimo()                            // Arimo 字体
///
/// Text('Title')
///   .fontSize(32.sp)
///   .textColor(ColorPalette.white)
///   .carterOne(weight: FontWeight.bold) // CarterOne 粗体
///
/// // 4. 链式调用组合使用
/// Text('Beautiful Text')
///   .fontSize(18.sp)
///   .textColor(ColorPalette.blue1)
///   .arimo(weight: FontWeight.w600)
///   .wrapAll(16.w)
///   .wrapHorizontal(24.w)
///
/// // 5. 在布局中使用
/// Container(
///   child: Column(
///     children: [
///       Text('Header')
///         .fontSize(20.sp)
///         .textColor(ColorPalette.black)
///         .arimo(weight: FontWeight.bold),
///       12.h.heightBox,
///       Text('Description')
///         .fontSize(14.sp)
///         .textColor(ColorPalette.black.withValues(alpha: 0.6))
///         .arimo()
///         .wrapHorizontal(16.w),
///     ],
///   ),
/// )
///
/// // 6. 添加点击效果 - WidgetTapEffectExtension
/// // 基本点击效果
/// Container(...)
///   .withTapEffect(
///     onTap: () => print('Tapped!'),
///     scaleFactor: 0.95,  // 缩放到 95%
///   )
///
/// // 轻微点击效果（缩放到 98%）
/// Container(...)
///   .withLightTap(onTap: () => handleTap())
///
/// // 明显点击效果（缩放到 92%）
/// Container(...)
///   .withStrongTap(onTap: () => handleTap())
///
/// // 弹性点击效果（带弹性动画）
/// Container(...)
///   .withBounceTap(onTap: () => handleTap())
///
/// // 7. 组合使用点击效果
/// Container(
///   padding: EdgeInsets.all(16.w),
///   decoration: BoxDecoration(
///     color: ColorPalette.blue1,
///     borderRadius: BorderRadius.circular(12.r),
///   ),
///   child: Text('Click Me')
///     .fontSize(16.sp)
///     .textColor(ColorPalette.white)
///     .arimo(weight: FontWeight.bold),
/// ).withTapEffect(
///   onTap: () => print('Button clicked'),
///   scaleFactor: 0.95,
/// )
/// ```

/// 数值扩展 - SizedBox间距
extension SizedBoxExtension on num {
  /// 创建宽度SizedBox
  Widget get widthBox => SizedBox(width: toDouble());

  /// 创建高度SizedBox
  Widget get heightBox => SizedBox(height: toDouble());

  /// 创建正方形SizedBox
  Widget get squareBox => SizedBox(width: toDouble(), height: toDouble());
}

/// Widget扩展 - 边距设置
extension WidgetSpacingExtension on Widget {
  /// 设置所有边距
  Widget wrapAll(double value) =>
      Padding(padding: EdgeInsets.all(value), child: this);

  /// 设置水平边距
  Widget wrapHorizontal(double value) =>
      Padding(padding: EdgeInsets.symmetric(horizontal: value), child: this);

  /// 设置垂直边距
  Widget wrapVertical(double value) =>
      Padding(padding: EdgeInsets.symmetric(vertical: value), child: this);

  /// 设置左边距
  Widget wrapLeft(double value) =>
      Padding(padding: EdgeInsets.only(left: value), child: this);

  /// 设置右边距
  Widget wrapRight(double value) =>
      Padding(padding: EdgeInsets.only(right: value), child: this);

  /// 设置上边距
  Widget wrapTop(double value) =>
      Padding(padding: EdgeInsets.only(top: value), child: this);

  /// 设置下边距
  Widget wrapBottom(double value) =>
      Padding(padding: EdgeInsets.only(bottom: value), child: this);
}

/// Widget扩展 - 字体设置
extension WidgetTextExtension on Widget {
  /// 设置字体大小
  Widget fontSize(double size) {
    if (this is Text) {
      final text = this as Text;
      return Text(
        text.data ?? '',
        style:
            text.style?.copyWith(fontSize: size) ?? TextStyle(fontSize: size),
        textAlign: text.textAlign,
        textDirection: text.textDirection,
        locale: text.locale,
        softWrap: text.softWrap,
        overflow: text.overflow,
        textScaleFactor: text.textScaleFactor,
        maxLines: text.maxLines,
        semanticsLabel: text.semanticsLabel,
        textWidthBasis: text.textWidthBasis,
        textHeightBehavior: text.textHeightBehavior,
        selectionColor: text.selectionColor,
      );
    }
    return this;
  }

  /// 设置CarterOne字体
  Widget carterOne({FontWeight? weight, List<Shadow>? shadows}) {
    if (this is Text) {
      final text = this as Text;
      return Text(
        text.data ?? '',
        style: GoogleFonts.carterOne(
          fontSize: text.style?.fontSize,
          fontWeight: weight ?? text.style?.fontWeight,
          color: text.style?.color,
          shadows: shadows ?? text.style?.shadows,
        ).merge(text.style),
        textAlign: text.textAlign,
        textDirection: text.textDirection,
        locale: text.locale,
        softWrap: text.softWrap,
        overflow: text.overflow,
        textScaleFactor: text.textScaleFactor,
        maxLines: text.maxLines,
        semanticsLabel: text.semanticsLabel,
        textWidthBasis: text.textWidthBasis,
        textHeightBehavior: text.textHeightBehavior,
        selectionColor: text.selectionColor,
      );
    }
    return this;
  }

  /// 设置Arimo字体
  Widget arimo({FontWeight? weight, List<Shadow>? shadows}) {
    if (this is Text) {
      final text = this as Text;
      return Text(
        text.data ?? '',
        style: GoogleFonts.arimo(
          fontSize: text.style?.fontSize,
          fontWeight: weight ?? text.style?.fontWeight,
          color: text.style?.color,
          shadows: shadows ?? text.style?.shadows,
        ).merge(text.style),
        textAlign: text.textAlign,
        textDirection: text.textDirection,
        locale: text.locale,
        softWrap: text.softWrap,
        overflow: text.overflow,
        textScaleFactor: text.textScaleFactor,
        maxLines: text.maxLines,
        semanticsLabel: text.semanticsLabel,
        textWidthBasis: text.textWidthBasis,
        textHeightBehavior: text.textHeightBehavior,
        selectionColor: text.selectionColor,
      );
    }
    return this;
  }

  /// 设置字体颜色
  Widget textColor(Color color) {
    if (this is Text) {
      final text = this as Text;
      return Text(
        text.data ?? '',
        style: text.style?.copyWith(color: color) ?? TextStyle(color: color),
        textAlign: text.textAlign,
        textDirection: text.textDirection,
        locale: text.locale,
        softWrap: text.softWrap,
        overflow: text.overflow,
        textScaleFactor: text.textScaleFactor,
        maxLines: text.maxLines,
        semanticsLabel: text.semanticsLabel,
        textWidthBasis: text.textWidthBasis,
        textHeightBehavior: text.textHeightBehavior,
        selectionColor: text.selectionColor,
      );
    }
    return this;
  }
}

/// Widget扩展 - 点击效果
extension WidgetTapEffectExtension on Widget {
  /// 添加点击缩放效果 - 提供视觉反馈
  ///
  /// [onTap] 点击回调函数
  /// [scaleFactor] 缩放比例，默认 0.96（缩小到96%）
  /// [duration] 动画持续时间，默认 200 毫秒
  /// [curve] 动画曲线，默认 Curves.easeInQuad
  ///
  /// 使用示例：
  /// ```dart
  /// Container(...)
  ///   .withTapEffect(
  ///     onTap: () => print('Tapped!'),
  ///   )
  /// ```
  Widget withTapEffect({
    required VoidCallback? onTap,
    double scaleFactor = 0.96,
    Duration? duration,
    Curve? curve,
  }) {
    return ZoomTapAnimation(
      onTap: onTap,
      // ignore: sort_child_properties_last
      child: this,
      end: scaleFactor,
      beginDuration: duration ?? const Duration(milliseconds: 200),
      endDuration: duration ?? const Duration(milliseconds: 200),
      beginCurve: curve ?? Curves.easeInQuad,
      endCurve: curve ?? Curves.easeInQuad,
    );
  }
}
